Conway's Game of Life

Charlie Veniot17th May 2022 at 11:46pm
' https://rosettacode.org/wiki/Conway%27s_Game_of_Life#GW-BASIC
' An Introduction to Conway's The Game of Life: https://www.youtube.com/watch?v=ouipbDkwHWA
' Test: BAM Compatibility with GW-BASIC
' Works as is despite screen mode 2 color incompatibility
' ๐ŸŸ  line 70 changed to zoom in on the drawn world
' USAGE: press "R" to generate a random world, then press and hold the space bar for animation

10 REM Conway's Game of Life
20 REM 30x30 grid, padded with zeroes as the boundary
30 DIM WORLD(31,  31, 1)
40 RANDOMIZE TIMER
50 CUR = 0 : BUF = 1
60 CLS
70 SCREEN _newimage(360, 64, 2) ' ๐ŸŸ  MODIFIED: original GW-BASIC code was:  70 SCREEN 2
80 LOCATE 5,10: PRINT "Press space to perform one iteration"
90 LOCATE 6,10: PRINT "B to load a blinker"
100 LOCATE 7,10: PRINT "R to randomise the world"
110 LOCATE 8,10: PRINT "Q to quit"
120 K$ = INKEY$
130 IF K$=" " THEN GOSUB 250: GOSUB 180
140 IF K$="B" OR K$="b" THEN GOSUB 400: GOSUB 180
150 IF K$="R" OR K$="r" THEN GOSUB 480: GOSUB 180
160 IF K$="Q" OR K$="q" THEN SCREEN 0: END
170 GOTO 120
180 REM draw the world
190 FOR XX=1 TO 30
200 FOR YY=1 TO 30
210 PSET (XX, YY), 15*WORLD(XX, YY, CUR)
220 NEXT YY
230 NEXT XX
240 RETURN
250 REM perform one iteration
260 FOR XX=1 TO 30
270 FOR YY=1 TO 30
280 SM=0
290 SM = SM + WORLD(XX-1, YY-1, CUR) + WORLD(XX, YY-1, CUR) + WORLD(XX+1, YY-1, CUR)
300 SM = SM + WORLD(XX-1, YY, CUR) + WORLD(XX+1, YY, CUR)
310 SM = SM + WORLD(XX-1, YY+1, CUR) + WORLD(XX, YY+1, CUR) + WORLD(XX+1, YY+1, CUR)
320 IF SM<2 OR SM>3 THEN WORLD(XX, YY, BUF) = 0
330 IF SM=3 THEN WORLD(XX, YY, BUF) = 1
340 IF SM=2 THEN WORLD(XX,YY,BUF) = WORLD(XX,YY,CUR)
350 NEXT YY
360 NEXT XX
370 CUR = BUF : REM exchange identities of current and buffer
380 BUF = 1 - BUF
390 RETURN
400 REM produces a vertical blinker at the top left corner, and blanks the rest
410 FOR XX=1 TO 30
420 FOR YY=1 TO 30
430 WORLD(XX,YY,CUR) = 0
440 IF XX=2 AND YY<4 THEN WORLD(XX, YY, CUR) = 1
450 NEXT YY
460 NEXT XX
470 RETURN
480 REM randomizes the world with a density of 1/2
490 FOR XX = 1 TO 30
500 FOR YY = 1 TO 30
510 WORLD(XX, YY, CUR) = INT(RND*2)
520 NEXT YY
530 NEXT XX
540 RETURN